<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="测试1" prop="Name1">
        <el-input v-model="queryParams.Name1" placeholder="请输入测试1" @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"/>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" show-overflow-tooltip>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="测试1" align="center" prop="Name1" />
      <el-table-column label="测试2" align="center" prop="Name2" />
      <el-table-column label="测试3" align="center" prop="Name3" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改测试demo对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
        <el-form-item label="测试1" prop="Name1">
          <el-input v-model="formData.Name1" placeholder="请输入测试1" />
        </el-form-item>
        <el-form-item label="测试2" prop="Name2">
          <el-input v-model="formData.Name2" placeholder="请输入测试2" />
        </el-form-item>
        <el-form-item label="测试3" prop="Name3">
          <el-input v-model="formData.Name3" placeholder="请输入测试3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Demo">
import { listDemo, getDemo, delDemo, addDemo, updateDemo } from "@/api/demo/demo"

const { proxy } = getCurrentInstance()
// const { sys_notice_status } = proxy.useDict('sys_notice_status')

const dataList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  formData: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    Name1: null,
  },
  rules: {
  }
})

const { queryParams, formData, rules } = toRefs(data)

/** 查询测试demo列表 */
const getList = async () => {
  loading.value = true
  let response = await listDemo(queryParams.value)
  loading.value = false
  dataList.value = response.data
  total.value = response.total
}

// 取消按钮
const cancel = () => {
  open.value = false
  reset()
}

// 表单重置
const reset = () => {
  formData.value = {
    Name1: null,
    Name2: null,
    Name3: null,
  }
  proxy.resetForm("formRef")
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.ID)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset()
  open.value = true
  title.value = "添加测试demo"
}

/** 修改按钮操作 */
const handleUpdate = async (row) => {
  reset()
  const ID = row.ID || ids.value[0]
  const response = await getDemo(ID)
  formData.value = response.data
  open.value = true
  title.value = "修改测试demo"
}

/** 提交按钮 */
const submitForm = async () => {
  const valid = await proxy.$refs["formRef"].validate()
  if (!valid) return
  let form = { ...formData.value }
  // 修改
  if (form.ID != null) {
    proxy.$modal.loading('正在保存...')
    await updateDemo(form)
    proxy.$modal.closeLoading()
    proxy.$modal.msgSuccess("修改成功")
    open.value = false
    await getList()
    return
  }
  // 新增
  proxy.$modal.loading('正在保存...')
  await addDemo(form)
  proxy.$modal.closeLoading()
  proxy.$modal.msgSuccess("新增成功")
  open.value = false
  await getList()
}

/** 删除按钮操作 */
const handleDelete = async (row) => {
  const IDS = row.ID ? [row.ID] : ids.value
  await proxy.$modal.confirm('是否确认删除？')
  await delDemo(IDS)
  proxy.$modal.msgSuccess("删除成功")
  await getList()
}

onMounted(() => {
  getList()
})
</script>
